package com.dbuil.bleshoes.base;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.LogUtils;
import com.lzy.okgo.OkGo;
import com.dbuil.bleshoes.Constant;
import com.dbuil.bleshoes.R;
import com.dbuil.bleshoes.bean.event.EventBean;
import com.dbuil.bleshoes.ui.view.LoadingDialog;
import com.dbuil.bleshoes.utils.BarUtil;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity {

    LoadingDialog myDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);//禁止截屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        BarUtil.setWhiteStatusBarForActivity(this);
        initView();
        initData();
        initListener();
    }

    public abstract int getLayoutId();

    public void initView() {
    }

    public void initData() {
    }

    public void initListener() {
    }

    @Override
    protected void onResume() {
        super.onResume();
/*
        String token = SPUtils.getInstance().getString(Constant.TOKEN);
        if (TextUtils.isEmpty(token) && !(this instanceof LoginActivity)&& !(this instanceof ShareTestActivity)&& !(this instanceof HomeActivity)) {
            LoginActivity.startLogin(this, false);
        }*/

    }

    /**
     * token失效后重新登陆
     */
    public void reLogin() {
        LogUtils.i("reLogin");
    }

    public void showProgress(String tip) {
        if (myDialog == null) {
            myDialog = new LoadingDialog(this, R.style.MyDialog);
        }
        myDialog.setCanceledOnTouchOutside(false);
        myDialog.show();

        if (!TextUtils.isEmpty(tip)) {
            myDialog.setDialogTip(tip);
        }

    }

    public void dismissProgress() {
        if (myDialog != null) {
            myDialog.dismiss();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshLogin(EventBean eventBean) {
        String msg = eventBean.getMsg();
        if (!TextUtils.isEmpty(msg) && TextUtils.equals(Constant.REFRESHLOGIN, msg)) {
            reLogin();
        }

    }




    @Override
    protected void onDestroy() {
        dismissProgress();
        super.onDestroy();
        OkGo.getInstance().cancelAll();
        EventBus.getDefault().unregister(this);

    }



    protected String getDownloadPath() {
        // 判断sd卡是否存在
        String path = null;

        //有外部存储,获取外部存储路径
        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())) {

            //  /storage/emulated/0/Android/data/包名/files/Download
            File sdDir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);// 获取根目录
            File dir = new File(sdDir + "/" + Constant.DOWNLOAD_PATH);
            if (!dir.exists()) {
                dir.mkdir();
            }
            path = dir + "/";
        } else {
            //没有外部存储，获取内部存储
            //  /data/data/包名/files
            File sdDir = getFilesDir();// 获取根目录
            File dir = new File(sdDir + "/" + Constant.DOWNLOAD_PATH);
            if (!dir.exists()) {
                dir.mkdir();
            }
            path = dir + "/";
        }


        return path;
    }

    protected static List<String> getUrl() {

        List<String> mUrls = new ArrayList<>();
        mUrls.add("http://e.hiphotos.baidu.com/image/pic/item/a1ec08fa513d2697e542494057fbb2fb4316d81e.jpg");
        mUrls.add("http://c.hiphotos.baidu.com/image/pic/item/30adcbef76094b36de8a2fe5a1cc7cd98d109d99.jpg");
        mUrls.add("http://h.hiphotos.baidu.com/image/pic/item/7c1ed21b0ef41bd5f2c2a9e953da81cb39db3d1d.jpg");
        mUrls.add("http://g.hiphotos.baidu.com/image/pic/item/55e736d12f2eb938d5277fd5d0628535e5dd6f4a.jpg");
        mUrls.add("http://e.hiphotos.baidu.com/image/pic/item/4e4a20a4462309f7e41f5cfe760e0cf3d6cad6ee.jpg");
        mUrls.add("http://b.hiphotos.baidu.com/image/pic/item/9d82d158ccbf6c81b94575cfb93eb13533fa40a2.jpg");
        mUrls.add("http://e.hiphotos.baidu.com/image/pic/item/4bed2e738bd4b31c1badd5a685d6277f9e2ff81e.jpg");
        mUrls.add("http://g.hiphotos.baidu.com/image/pic/item/0d338744ebf81a4c87a3add4d52a6059252da61e.jpg");
        mUrls.add("http://a.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee5080c8142ff5e0fe99257e19.jpg");
        mUrls.add("http://f.hiphotos.baidu.com/image/pic/item/4034970a304e251f503521f5a586c9177e3e53f9.jpg");
        mUrls.add("http://b.hiphotos.baidu.com/image/pic/item/279759ee3d6d55fbb3586c0168224f4a20a4dd7e.jpg");
        mUrls.add("http://img2.xkhouse.com/bbs/hfhouse/data/attachment/forum/corebbs/2009-11/2009113011534566298.jpg");
        mUrls.add("http://a.hiphotos.baidu.com/image/pic/item/e824b899a9014c087eb617650e7b02087af4f464.jpg");
        mUrls.add("http://c.hiphotos.baidu.com/image/pic/item/9c16fdfaaf51f3de1e296fa390eef01f3b29795a.jpg");
        mUrls.add("http://d.hiphotos.baidu.com/image/pic/item/b58f8c5494eef01f119945cbe2fe9925bc317d2a.jpg");
        mUrls.add("http://h.hiphotos.baidu.com/image/pic/item/902397dda144ad340668b847d4a20cf430ad851e.jpg");
        mUrls.add("http://b.hiphotos.baidu.com/image/pic/item/359b033b5bb5c9ea5c0e3c23d139b6003bf3b374.jpg");
        mUrls.add("http://a.hiphotos.baidu.com/image/pic/item/8d5494eef01f3a292d2472199d25bc315d607c7c.jpg");
        mUrls.add("http://b.hiphotos.baidu.com/image/pic/item/e824b899a9014c08878b2c4c0e7b02087af4f4a3.jpg");
        mUrls.add("http://g.hiphotos.baidu.com/image/pic/item/6d81800a19d8bc3e770bd00d868ba61ea9d345f2.jpg");
        return mUrls;
    }



    /**
     * 隐藏虚拟按键
     */
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    /**
     * 恢复普通状态
     */
    private void showBottomUIMenu() {

        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.VISIBLE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SCREEN_STATE_OFF;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }




    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        //非默认值
        if (newConfig.fontScale != 1) {
            getResources();
        }
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        //还原字体大小
        Resources res = super.getResources();
        //非默认值
        if (res.getConfiguration().fontScale != 1) {
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();
            //设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }


}
